package com.itheima.health.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting OrderSetting){
        orderSettingService.editNumberByDate(OrderSetting);
        return new Result(true, MessageConst.ORDER_SUCCESS);
    }
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer  year,Integer  month){


        List<Map> mapList =orderSettingService.getOrderSettingByMonth(year,month);

        return new Result(true, MessageConst.ACTION_SUCCESS,mapList);

    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> excel = POIUtils.readExcel(excelFile);
            if (excel !=null && excel.size()>0){
                List<OrderSetting> orderSettingList=new ArrayList<>();
                for (String[] strings : excel) {
                    SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
                    OrderSetting orderSetting = new OrderSetting(ft.parse(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);

                }
                orderSettingService.insert(orderSettingList);

            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }


        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
}
